package com.legal.service.chat.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.legal.domain.dto.CasesDTO;
import com.legal.domain.po.Cases;
import com.legal.mapper.CasesMapper;
import com.legal.service.chat.ICasesService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 案件基本信息表 服务实现类
 * </p>
 *
 * @author xiaowu
 * @since 2025-06-25
 */
@Service
public class CasesServiceImpl extends ServiceImpl<CasesMapper, Cases> implements ICasesService {

    /**
     * 根据用户编号和chatId查询案件信息
     * @param userNo
     * @param chatId
     * @return
     */
    @Override
    public Cases getCases(String userNo, String chatId) {
        Cases one = lambdaQuery()
                .eq(Cases::getUserNo, userNo)
                .eq(Cases::getChatId, chatId)
                .one();
        return one;
    }

    /**
     * 根据用户编号查询案件信息
     * @param userNo
     * @return
     */
    @Override
    public List<Cases> getAllCasesByUserNo(String userNo) {
        List<Cases> list = lambdaQuery()
                .eq(Cases::getUserNo, userNo)
                .list();
        return list;
    }

    /**
     * 根据案件id更新案件内容
     * @param id
     * @param content
     */
    @Override
    public void updateCaseContent(Integer id, String content) {
        lambdaUpdate()
                .eq(Cases::getId, id)
                .set(content!=null,Cases::getCaseContent, content)
                .set(Cases::getEvidenceUpload,"已完成")
                .update();
    }

    /**
     * 根据chatId更新案件信息
     * @param chatId
     * @param vectoryRate
     */
    @Override
    public void updateCase(String chatId, Integer vectoryRate) {
        lambdaUpdate()
                .eq(Cases::getChatId, chatId)
                .set(Cases::getAnalysisStatus, "已分析")
                .set(vectoryRate!=null,Cases::getVictoryRate, vectoryRate)
                .update();
    }


    /**
     * 添加案件信息
     * @param casesDTO
     */
    @Override
    public void addCase(CasesDTO casesDTO) {
        Cases cases = BeanUtil.copyProperties(casesDTO, Cases.class);
        cases.setCreateTime(LocalDate.now());
        cases.setId(null);
        save(cases);
    }

    /**
     * 根据案件id更新案件信息
     * @param casesDTO
     */
    @Override
    public void updateCaseById(CasesDTO casesDTO) {
        lambdaUpdate()
                .eq(Cases::getId, casesDTO.getId())
                .set(casesDTO.getCaseName()!=null,Cases::getCaseName, casesDTO.getCaseName())
                .set(casesDTO.getCaseProcedure()!=null,Cases::getCaseProcedure, casesDTO.getCaseProcedure())
                .update();
    }

    /**
     * 添加案件的chatId，用作第一次对话
     * @param chatId
     * @param id
     */
    @Override
    public void addChatId(String chatId, Integer id) {
        lambdaUpdate()
                .eq(Cases::getId, id)
                .set(chatId!=null,Cases::getChatId, chatId)
                .update();
    }

}
